/**
 * 判断是否是外链
 * @param {string} path
 * @returns {Boolean}
 */
export const isExternal = (path: string): boolean => {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * 判断中英文长度是否超过限制
 * @param {string} str
 * @param {string} len
 * @returns {Boolean}
 */
export const isOverLength = (str) => {
  let i, sum
  sum = 0
  for (i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) >= 0 && str.charCodeAt(i) <= 255) {
      sum = sum + 1
    } else {
      sum = sum + 2
    }
  }
  return sum
}

/**
 * @param {string} strNum
 * @returns {Boolean}
 */
export function isStrNumber(strNum) {
  const reg = /^[0-9]*$/
  return reg.test(strNum)
}

/**
 * @description: 全为数字组成
 * @param {*}
 * @return {*}
 */
export const validateStrNumber = (rule, value) => {
  if (!isStrNumber(value) && value !== undefined) {
    return Promise.reject(new Error('必须全为数字'))
  } else {
    return Promise.resolve()
  }
}

/**
 * @param {string} money
 * @returns {Boolean}
 */
export function isMoney(money) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  return reg.test(money)
}

/**
 * @description: 金钱校验
 * @param {*}
 * @return {*}
 */
export const validateMoney = (rule, value) => {
  if (!isMoney(value) && value !== undefined) {
    return Promise.reject(new Error('只能输入数字或者至多两位小数'))
  } else {
    return Promise.resolve()
  }
}

/**
 * E-mail地址校验
 * @param {string} email
 * @returns {Boolean}
 */
export function isEmail(email) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * URL地址校验
 * @param {string} url
 * @param {Boolean} allowChinese 是否允许包含中文
 * @returns {Boolean}
 */
export function isURL(url, allowChinese = true) {
  if (allowChinese) {
    const reg = /https?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/
    return reg.test(url)
  } else {
    const reg =
      /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
    return reg.test(url)
  }
}

/**
 * 手机号校验
 * @param {string} mobilePhone
 * @returns {Boolean}
 */
export function isMobilePhone(mobilePhone) {
  const reg = /^1[3456789]\d{9}$/
  return reg.test(mobilePhone)
}

/**
 * 座机校验
 * @param {string} telephone
 * @returns {Boolean}
 */
export function isTelephone(telephone) {
  const reg = /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/
  return reg.test(telephone)
}

/**
 * @description: 完整校验座机
 * @param {*}
 * @return {*}
 */
export const validateTelephone = (rule, value) => {
  if (!isTelephone(value)) {
    return Promise.reject(new Error('请输入正确的座机'))
  } else {
    return Promise.resolve()
  }
}

/**
 * @description: 模糊校验手机号
 * @param {*}
 * @return {*}
 */
export const validateMobilePhoneBlur = (rule, value) => {
  if (value !== undefined) {
    const first = Number(value.charAt(0))
    if (first !== 1) {
      return Promise.reject(new Error('请输入正确的手机号'))
    } else {
      return Promise.resolve()
    }
  } else {
    return Promise.resolve()
  }
}

/**
 * @description: 完整校验手机号
 * @param {*}
 * @return {*}
 */
export const validateMobilePhone = (rule, value) => {
  if (!isMobilePhone(value)) {
    return Promise.reject(new Error('请输入正确的手机号'))
  } else {
    return Promise.resolve()
  }
}

/**
 * @description: 完整校验手机号/座机
 * @param {*}
 * @return {*}
 */
export const validatePhone = (rule, value) => {
  if (!isTelephone(value) && !isMobilePhone(value)) {
    return Promise.reject(new Error("请输入正确的座机或手机号，区号请加 '-' "))
  } else {
    return Promise.resolve()
  }
}

/**
 * 中文校验
 * @param {string} chineseText
 * @returns {Boolean}
 */
export function validChineseText(chineseText) {
  const reg = /(^[\u4e00-\u9fa5]{0,}$)/
  return reg.test(chineseText)
}

/**
 * @description: 校验中文姓名
 * @param {*}
 * @return {*}
 */
export const validateHumanName = (rule, value) => {
  if (!validChineseText(value) && value !== undefined) {
    return Promise.reject(new Error('请输入中文姓名'))
  } else {
    return Promise.resolve()
  }
}

/**
 * @description: 校验中文地图地址
 * @param {*}
 * @return {*}
 */
export const validateMapAddress = (rule, value) => {
  if (value.indexOf('省') < 0 && value.indexOf('自治区') < 0) {
    return Promise.reject(new Error('请输入省/自治区级行政单位名称'))
  } else if (value.indexOf('市') < 0) {
    return Promise.reject(new Error('请输入市级行政单位名称'))
  } else if (value.indexOf('区') < 0 && value.indexOf('县') < 0) {
    return Promise.reject(new Error('请输入城区/县级行政单位名称'))
  } else if (value.length < 9) {
    return Promise.reject(new Error('地址长度至少为9个字'))
  } else {
    return Promise.resolve()
  }
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function isQQVideoUrl(url) {
  const reg = /v.qq.com?/
  return reg.test(url)
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function includeHTML(url) {
  const reg = /.html?/
  return reg.test(url)
}

/**
 * @description: 校验广告地址
 * @param {*}
 * @return {*}
 */
export const validateVideoUrl = (rule, value) => {
  if (!isURL(value) && value !== undefined && value !== '') {
    return Promise.reject(new Error('该地址不是以http://或者https://开头或不是网址'))
  } else if (!isQQVideoUrl(value) && value !== undefined && value !== '') {
    return Promise.reject(new Error('该地址不是腾讯视频的网址'))
  } else if (!includeHTML(value) && value !== undefined && value !== '') {
    return Promise.reject(new Error('该地址后缀不包含html'))
  } else {
    return Promise.resolve()
  }
}
